package com.example.controller;

import com.example.pojo.Goods;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.constant.GoodsState;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    // 获取商品列表（可分页、按分类、按状态筛选）
    @GetMapping
    public Result<PageBean<Goods>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer cateId,
            @RequestParam(required = false) String stuName,
            @RequestParam(required = false) String state
    ) {
        PageBean<Goods> pb = goodsService.list(pageNum, pageSize, name ,cateId, stuName, state);
        return Result.success(pb);
    }

    // 查看商品详情
    @GetMapping("/detail")
    public Result<Goods> detail(@RequestParam Integer id) {
        Goods goods = goodsService.findById(id);
        return Result.success(goods);
    }

    // 修改商品状态（全局操作）
    @PutMapping("/state")
    public Result updateState(@RequestBody Goods goods) {
        goodsService.updateState(goods);
        return Result.success();
    }


}